<template>
  <div>
    <van-nav-bar
      :title="$route.name"
      @click-left="$router.back()"
      left-text="返回"
      left-arrow
      right-text="注册"
      @click-right="$router.push('/register')"
    />
    <!-- 输入表单 -->
    <van-form @submit="login">
      <van-field
        v-model="userlist.phone"
        name="手机号"
        label="手机号"
        placeholder="请输入手机号"
        :rules="[{ required: true, message: '请输入手机号' }]"
      />
      <van-field
        v-model="userlist.password"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px;">
        <van-button round block type="info" native-type="submit">登录</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import {Toast} from 'vant'
import {login} from '../request/api'
export default {
  data() {
    return {
        userlist:{
            phone:"",
            password:"",
        }
    };
  },
  methods:{
      //登录事件
      login(){
          login(this.userlist).then((res)=>{
              if(res.code==200){
                  this.$store.dispatch("changeUser",res.list);
                  Toast.success(res.msg)
                  this.$router.push("/home")
              }else{
                  Toast.fail(res.msg)
              }
          })
      }
  }
};
</script>

<style scoped>
</style>
